package org.artifact.csv;

import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import lombok.Builder;
@Builder
public class CsvBuilder {
	/** 模板组 */
	private TemplateConfig templateConfig;

	/** 生成的源码路径 */
	private String sourcePath;

	/** 生成源码的包名称 */
	private String packageName;
	
	/** 设计文件路径 */
	private String designSourcePath;
	
	
	private Map<String, String> exts = new HashMap<>();
	
	/**
	 * 使用默认模板组
	 * 
	 * @return
	 */
	public void useDefaultGroupTemplate() {
		this.templateConfig = new TemplateConfig();
		this.templateConfig.setCharset(CharsetUtil.CHARSET_UTF_8);
		this.templateConfig.setResourceMode(ResourceMode.CLASSPATH);
	}
	
	public void exec() throws Exception {
		if (this.templateConfig == null) {
			useDefaultGroupTemplate();
		}
		
		if (StrUtil.isBlank(sourcePath) || StrUtil.isBlank(packageName) || designSourcePath == null) {
			throw new IllegalStateException("请检查配置");
		}

		check();

//		verify();

		execute();
		
		completeInfo();
	}

	private void execute(){
		TemplateEngine engine = TemplateUtil.createEngine(this.templateConfig);
		
		Template template = engine.getTemplate("csv.btl");

		List<File> files = FileUtil.loopFiles(designSourcePath,new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return StrUtil.endWith(pathname.getName(), ".csv");
			}
		});
		
		List<CsvBean> csvs = new ArrayList<>();
		for (File file : files) {
			CsvBean csv = new CsvBean(file,exts.getOrDefault(file.getName(), ""));
			
			Map<String, Object> bindingMap = new HashMap<>();
			bindingMap.put("csv", csv);
			bindingMap.put("packageName", packageName);
			bindingMap.put("fileName", file.getName());
			
			csvs.add(csv);
			
			String pathname = sourcePath + File.separator + csv.getName() + "Csv.java";
			render(pathname, template, bindingMap);
		}
	}

	private void render(String path,Template template, Map<String, Object> bindingMap) {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(new File(path));
			template.render(bindingMap, fileWriter);
			fileWriter.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void check() {

		if(!FileUtil.isDirectory(designSourcePath)) {
			throw new IllegalStateException("不是目录");
		}
		
		File file = new File(sourcePath);
		if (!file.exists() || !file.isDirectory()) {
			file.mkdirs();
		}
	}
	
	/**
	 * 成功打印信息
	 */
	public static void completeInfo() {
		System.out.println("*********************************************");
		System.out.println("*                                           *");
		System.out.println("**********代码生成完成-请刷新项目！**********");
		System.out.println("*                                           *");
		System.out.println("*********************************************");
		System.exit(0);
	}
}
